<?php
namespace app\admin\service;

use \think\Db;
use think\Model;
use app\model\UserMenu;

class Menu extends Model
{
    protected $UserModel;
    protected function initialize()
    {
        parent::initialize();
        $this->UserModel = new UserMenu();
    }

    /*获取菜单列表*/
    public function getMenuList()
    {
        $list = $this->UserModel->order('sort')->select();
        return $list;
    }

    /*获取上级菜单*/
    public function getParentMenu($menu_level)
    {
        $data = $this->UserModel
            ->where('menu_level = '. $menu_level)
            ->order('sort')
            ->select();
        return $data;
    }


    /*编辑保存菜单*/
    public function saveMenuList($list,$id)
    {
        if(!$id){
            if($this->UserModel->save($list)){
                return 1;
            }else{
                return 0;
            }
        }else{
            $i['id']=$id;
            if($this->UserModel->save($list,$i)){
                return 1;
            }else{
                return 0;
            }
        }
    }

    /*获取菜单*/
    public function getMenuById($id)
    {
        $data =  $this->UserModel->get($id);
        $data['parent_name'] = $this->UserModel->where('id',$data['parent_id'])->value('menu_name');
        return $data;
    }

    /*删除菜单*/
    public function deleteMenuById($id)
    {
        $menu = $this->UserModel->get($id);
        if( $menu->delete()){
            return 1;
        }else {
            return 0;
        }
    }











}
